/*
    Copyright 2009 LdM
    
	This file is part of MediaPlayer.

    MediaPlayer is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MediaPlayer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with MediaPlayer.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.ldm.mediaplayer;

import java.awt.Component;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.IncompatibleSourceException;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoDataSourceException;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.ResourceUnavailableEvent;
import javax.media.protocol.DataSource;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class MediaPlayer extends JFrame implements ControllerListener {

	Player player;
	Component visual;
	
	public MediaPlayer() {
		super("MediaPlayer");
		this.setSize(500, 500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
	}
	
	private void init(File file) {
		try {
			URI uri = file.toURI(); 
			DataSource source = Manager.createDataSource(
					uri.toURL());	
			player = Manager.createPlayer(source);
			player.addControllerListener(this);
		} catch (NoPlayerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoDataSourceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//this.getContentPane().add(player.getVisualComponent());
	}

	private void launch() {		
		this.setVisible(true);
		player.start();
		/*
		try {
			DataSource source = Manager.createDataSource(new URL("D:\\film\\nouveaux films\\Walle French.avi"));
			player.setSource(source);
			this.setVisible(true);
			player.start();
		} catch (NoDataSourceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IncompatibleSourceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}

	public static void main(String[] args) {
		MediaPlayer mp = new MediaPlayer();
		JFileChooser chooser = new JFileChooser();		
		if (chooser.showOpenDialog(mp) == JFileChooser.APPROVE_OPTION) {
			File file = chooser.getSelectedFile();
			mp.init(file);
			mp.launch();
		}
	}

	@Override
	public synchronized void controllerUpdate(ControllerEvent controllerevent) {
		System.out.println("EVENT:"+controllerevent.toString());
		if (controllerevent instanceof ResourceUnavailableEvent) {
			System.out.println("CANNOT PLAY");
			System.exit(0);
		}
		if (player.getState() >= Player.Realized) {
			if (visual == null) {
				visual = player.getVisualComponent();
				System.out.println("Realized"+visual);
				if (visual != null) {
					this.getContentPane().add(visual);
					 Dimension videoSize = visual.getPreferredSize();
					    int videoWidth = videoSize.width;
					    int videoHeight = videoSize.height;
					//    width = videoWidth;
					  //  height += videoHeight;
					    visual.setBounds(0, 0, videoWidth, videoHeight);

				}
			}
		}
	}

}
